package com.projectxxx.persistence;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {

	// table toys
	public static final String TABLE_TOYS = "devices";
	public static final String[] TABLE_TOYS_COLUMNS = { DatabaseHelper.ID,
			"macaddress", "name" };
	// table users
	public static final String TABLE_USERS = "users";
	public static final String[] TABLE_USERS_COLUMNS = { DatabaseHelper.ID,
			"username", "email" };

	// database specific constants
	public static final String ID = "id";
	private static final String DATABASE_NAME = "app.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String USERS_CREATE = "create table " + TABLE_USERS
			+ "(" + ID + " integer primary key autoincrement, "
			+ "username text not null," + "email text not null);";
	private static final String TOYS_CREATE = "create table " + TABLE_TOYS
			+ "(" + ID + " integer primary key autoincrement,"
			+ "mac text not null," + "name text not null);";

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(USERS_CREATE);
		database.execSQL(TOYS_CREATE);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.w(DatabaseHelper.class.getName(), "Upgrading database from version "
				+ oldVersion + " to " + newVersion
				+ ", which will destroy all old data");
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_TOYS);
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
		onCreate(db);
	}

}
